<?php

/**
 * wxz_eat模块微站定义
 *
 * @author vipl520
 * @url
 */
defined('IN_IA') or exit('Access Denied');

class Wxz_eatModuleSite extends WeModuleSite
{

    public function doMobileIndex()
    {
        global $_GPC, $_W;
        // checkauth();
        if (!$_W['openid']) {
            message('请在微信中打开');
        }
        if (empty($_W['fans']['nickname'])) {
            mc_oauth_userinfo(); //未关注用户借用
        }
        $url = $this->resuorce();
        $setting = $this->module['config'];
        $isfollow = 1;
        if (!$_W['fans']['follow'] && $setting['isgz']) {
            $isfollow = 0;
        }
        $xxz = $this->xzpz($setting['xxzpz']);
        $zxz = $this->xzpz($setting['zxzpz']);
        $dxz = $this->xzpz($setting['dxzpz']);
        include $this->template('index');
    }

    public function doMobileApi()
    {
        // 初始化
        global $_GPC, $_W;

        $score = pdo_fetch("SELECT * FROM " . tablename('wxz_eat_score') . " WHERE openid = :openid", array(':openid' => $_W['fans']['openid']));
        //分数上传接口
        if ($_GPC['op'] == 'scoresup'&&$_W['fans']['tag']['avatar']&&$_W['fans']['tag']['nickname']) {

            if ($score['id']) {
                //比较大小
                $score['maxscores'] = ($score['maxscores'] > $_GPC['scores']) ? $score['maxscores'] : $_GPC['scores'];
                $arr = array(
                    'scores' => $_GPC['scores'],
                    'maxscores' => $score['maxscores'],
                    'avatar' => $_W['fans']['tag']['avatar'],
                    'datetime' => TIMESTAMP,
                );
                $result = pdo_update('wxz_eat_score', $arr, array('id' => $score['id']));
                if (!empty($result)) {
                    echo ('202');
                }

            } else {
                $arr = array(
                    'openid' => $_W['fans']['openid'],
                    'nickname' => $_W['fans']['tag']['nickname'],
                    'avatar' => $_W['fans']['tag']['avatar'],
                    'scores' => $_GPC['scores'],
                    'maxscores' => $_GPC['scores'],
                    'datetime' => TIMESTAMP,
                );
                $result = pdo_insert('wxz_eat_score', $arr);
                if (!empty($result)) {
                    echo ('201');
                }

            }

        }
        //分享次数接口
        if ($_GPC['op'] == 'shareup') {
            if ($score['id']) {
                //比较大小
                $score['share']++;
                $arr = array(
                    'share' => $score['share'],
                    'datetime' => TIMESTAMP,
                );
                $result = pdo_update('wxz_eat_score', $arr, array('id' => $score['id']));
                if (!empty($result)) {
                    echo ('202');
                }

            } else {
                $arr = array(
                    'openid' => $_W['fans']['openid'],
                    'nickname' => $_W['fans']['tag']['nickname'],
                    'share' => 1,
                    'datetime' => TIMESTAMP,
                );
                $result = pdo_insert('wxz_eat_score', $arr);
                if (!empty($result)) {
                    echo ('201');
                }

            }
        }

        if ($_GPC['op'] == 'rank') {
            $query1 = load()->object('query');
            $myScores = $query1->from('wxz_eat_score')->where(array('openid =' => $_W['fans']['openid']))->get();
            $myRank = $query1->from('wxz_eat_score')->where(array('maxscores >' => $myScores['maxscores']))->count();
            $myStr = "<tr style='background: #fbdec9;'><td>" . $myRank . "</td><td><img src='" . $myScores['avatar'] . "'></td><td>" . $myScores['nickname'] . "</td><td>" . $myScores['scores'] . "</td><td>" . $myScores['maxscores'] . "</td></tr>";
            $query2 = load()->object('query'); //这边需要重新加载
            $ranklist = $query2->from('wxz_eat_score')->limit(0, 6)->orderby('maxscores', 'DESC')->getall();
            $str = "";
            foreach ($ranklist as $key => $value) {
                $key = $key + 1;
                if ($value['openid'] == $_W['fans']['openid']) {
                    $my = "style='background: #fbdec9;'";
                } else {
                    $my = "";
                }

                $str .= "<tr " . $my . "><td>" . $key . "</td><td><img src='" . $value['avatar'] . "'></td><td>" . $value['nickname'] . "</td><td>" . $value['scores'] . "</td><td>" . $value['maxscores'] . "</td></tr>";
            }

            if ($myRank > 6) {
                $str = $str . $myStr;
            }
            echo ($str);
        }
    }

    public function doWebList()
    {
		global $_GPC, $_W;
		$url = $this->createWebUrl('list');
        $size = 10;
        $query = load()->object('query');
        $count = $query->from('wxz_eat_score')->count();
        $content = $query->from('wxz_eat_score')->page($_GPC['page'], $size)->orderby('id', 'DESC')->getall();
        $page = pagination($count, $_GPC['page'], $size);

        if ($_GPC['op'] == 'del' && $_GPC['wid']) {
            if (pdo_delete('wxz_eat_score', array('id' => $_GPC['wid']))) {
                message('删除成功',$url,"success");
            }
        }

        include $this->template('list');
    }

    public function resuorce()
    {
        global $_GPC, $_W;
        //修复分享后链接打不开
        $url = array(
            'style' => $_W['siteroot'] . "addons/wxz_eat/template/mobile/style",
            'icon' => $_W['siteroot'] . 'addons/wxz_eat/icon.jpg',
            'link' => $_W['siteroot'] . 'app/' . $this->createMobileUrl('index'),
            'api' => $this->createMobileUrl('api'),
        );
        return $url;
    }
    public function xzpz($xzpz)
    {
        $xzpz = str_replace(array("\r\n", "\n", "\r"), '/hhf/', $xzpz); //替换回车空格为/hhf/
        list($hp, $sd, $df) = explode("/hhf/", $xzpz);
        $hp = $this->strpos_pz($hp);
        $sd = $this->strpos_pz($sd);
        $df = $this->strpos_pz($df);
        $xzpz = array(
            'hp' => $hp,
            'sd' => $sd,
            'df' => $df,
        );
        return $xzpz;
    }

    public function strpos_pz($pz)
    {
        $pz = substr($pz, 3);
        if (strpos($pz, "||")) {
            list($pzx, $pzd) = explode("||", $pz);
            $pz = "random(" . $pzx . "," . $pzd . ")";
        }
        return $pz;
    }

}
